﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pwblog.BusinessObject;

namespace pwblog.Web.Management
{
    public partial class ManageCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FillCategoriesGrid();
            }
        }

        /// <summary>
        /// Tải dữ liệu lên GridView
        /// </summary>
        protected void FillCategoriesGrid()
        {
            var categories = new CategoryBo().GetCategories();
            if (categories != null)
            {
                gvCategories.DataSource = categories;
                gvCategories.DataBind();
            }
        }

        /// <summary>
        /// xử lí sự kiện gvCategories_RowCommand khi click "Insert"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewRowEventArgs</param>
        protected void gvCategories_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Insert"))
            {
                string name = ((TextBox)gvCategories.FooterRow.FindControl("txtName")).Text.Trim();
                if (new CategoryBo().InsertCategory(name))
                {
                    FillCategoriesGrid();
                    // thanh cong
                }
                else
                {
                    // that bai
                }
            }
        }

        /// <summary>
        /// Sử lí sự kiện gvCategories_RowEditing khi click vào "Edit"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewEditEventArgs</param>
        protected void gvCategories_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvCategories.EditIndex = e.NewEditIndex;
            FillCategoriesGrid();
        }

        /// <summary>
        /// Xử lí sự kiện gvCategories_RowDeleting khi click vào "Delete"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewDeleteEventArgs</param>
        protected void gvCategories_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int idCategory = Int32.Parse(gvCategories.DataKeys[e.RowIndex].Values[0].ToString());
            if (new CategoryBo().DeleteCategory(idCategory))
            {
                // thanh cong
                FillCategoriesGrid();
            }
            else
            {
                // that bai
            }
            
        }

        /// <summary>
        /// Xử lí sự kiện gvCategories_RowUpdating khi click "Update"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewUpdateEventArgs</param>
        protected void gvCategories_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = Int32.Parse(gvCategories.DataKeys[e.RowIndex].Values[0].ToString());
            string name = ((TextBox)gvCategories.Rows[e.RowIndex].FindControl("txtName")).Text.Trim();
            if (new CategoryBo().UpdateCategory(id, name))
            {
                // thanh cong
            }
            else
            {
                // that bai
            }
            gvCategories.EditIndex = -1;
            FillCategoriesGrid();
        }

        /// <summary>
        /// Xử lý sự kiện gvCategories_RowCancelingEdit khi click vào "Cancel"
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewCancelEditEventArgs</param>
        protected void gvCategories_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvCategories.EditIndex = -1;
            FillCategoriesGrid();
        }

        /// <summary>
        /// Xử lí sự kiện gvCategories_PageIndexChanging khi chuyển trang
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">GridViewPageEventArgs</param>
        protected void gvCategories_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCategories.PageIndex = e.NewPageIndex;
            FillCategoriesGrid();
        }
    }
}